//
//  moveTableViewController.m
//  11210
//
//  Created by student on 16/3/4.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "moveTableViewController.h"

@interface moveTableViewController ()
@property(nonatomic,strong)NSMutableArray *list;
@end

@implementation moveTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = [@[@"Eeny", @"Meeny", @"Miney", @"Moe", @"Catch", @"A",
                   @"Tiger", @"By", @"The", @"Toe"] mutableCopy];
    UIBarButtonItem *movebtn=[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(togglemove)];
    self.navigationItem.rightBarButtonItem=movebtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)togglemove
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"完成":@"编辑"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text=self.list[indexPath.row];
    
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *obj=self.list[sourceIndexPath.row];
    [self.list removeObject:obj];
    [self.list insertObject:obj atIndex:destinationIndexPath.row];
}
@end
